import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from sklearn.metrics import classification_report
from sklearn.metrics import roc_curve
from sklearn.linear_model import LogisticRegression
from sklearn import svm
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from vis_function import *


def create_model():
    # 导入cancer数据
    global x_train, x_test, y_train, y_test, x_total, y_total
    io_total = r'class_total.xlsx'
    total = pd.read_excel(io_total)  # pandas内置格式文件
    x_total = total.iloc[:, [2, 5]]  # 选择两列特征进行分类
    y_total = total.iloc[:, 7]
    x_total = np.array(x_total.values.tolist())  # 转为能进行计算的numpy数组格式

    io_train = r'class_train.xlsx'
    train = pd.read_excel(io_train)
    x_train = train.iloc[:, [2, 5]]
    y_train = train.iloc[:, 7]
    x_train = np.array(x_train.values.tolist())
    print("x_train")
    print(x_train)
    print("y_train")
    print(y_train)

    io_test = r'class_test.xlsx'
    test = pd.read_excel(io_test)
    x_test = test.iloc[:, [2, 5]]
    y_test = test.iloc[:, 7]
    x_test = np.array(x_test.values.tolist())
    print("x_test")
    print(x_test)
    print("y_test")
    print(y_test)

    # 创建模型
    name = input('Please enter the classification method:（1）LogisticRegression or （2）SVM or （3）LDA\n')
    clf = get_clsFunc(name)  # 选取分类器
    global model
    model = clf.fit(x_train, y_train)  # 训练分类器
    model.coef_  # 特征系数
    print("特征系数：", model.coef_)
    model.intercept_  # 截距
    print("截距：", model.intercept_)


# 模型预测
def predict_model():
    print("模型预测：")
    global y_pre, proba
    y_pre = model.predict(x_test)
    print(model.predict(x_test))  # 数据的类别
    proba = model.predict_proba(x_test)
    print(model.predict_proba(x_test))  # 数据的概率


# 精度报告
def clf_report():
    cla_name = ['0', '1']
    print('精度报告：')
    print(classification_report(y_test, y_pre, target_names=cla_name))


def vis():
    roc_plot(y_test, proba[:, 1])
    plot_decisionBoundary(x_total, y_total, model)


if __name__ == "__main__":
    create_model()
    predict_model()
    clf_report()
    vis()
